<?php
//global variables

$_month_names = array('','Jan','Feb','Mac','Apr','Mei','Jun','Jul','Ogos','Sept','Okt','Nov','Dis');
$_month_lgnames = array(1=>'Januari','Februari','Mac','April','Mei','Jun','Julai','Ogos','September','Oktober','November','Disember');
$_range_months = array();
for ($i=1;$i<=12;$i++) {
$k = $_month_names[$i]; 
	$_months["$k"] = $i;
}

$weekday_arr = array( 'Monday'    => 'Isnin',
                      'Tuesday'   => 'Selasa',
                      'Wednesday' => 'Rabu',
                      'Thursday'  => 'Khamis',
                      'Friday'    => 'Jumaat',
                      'Saturday'  => 'Sabtu',
                      'Sunday'    => 'Ahad',
                      'Holiday'   => 'Cuti'
);
$weekdays = array('Sunday',
                  'Monday',
				  'Tuesday',
				  'Wednesday',
				  'Thursday',
				  'Friday',
				  'Saturday'
				  );


$_genders = array("Male" => "Lelaki", "Female" => "Perempuan");

$PERINGKAT_ARRAY = array( "1"  => "KPM"  ,
                          "2"  => "JPN"  ,
						  "3"  => "PPD"  ,
						  "4"  => "INSTITUSI",
						  "5"  => "INSTITUSI"
						 );
						 
function alert_flash_message() { 
	if ($_SESSION['flash'] != '') {
	$str = "<center>".
           "<span id=\"flash\" style=\"width:30%; background-color: #009933; color: white; font-weight: ; padding: 4px;\">".
		   $_SESSION['flash'].
		   "</span></center>";
	echo $str;
	$_SESSION['flash'] = '';
	}
}

function alert_debug($msg) {
	$ENABLE_DEBUG = 1;
	if ($ENABLE_DEBUG == 1) {
		echo "<script>alert('$msg')</script>";
	}
}

function dmy_date($ymd_date) {
	if ($ymd_date!='') {
		$d_arr = split("-",$ymd_date);
		return "$d_arr[2]-$d_arr[1]-$d_arr[0]";
	}
	else
		return '';
}

function ymd_date($dmy_date) {
	
	if ($dmy_date!='') {
		$d_arr = split("-",$dmy_date);
		return "$d_arr[2]-$d_arr[1]-$d_arr[0]";
	}
	else
		return '';
}

function mdytoymd_date($mdy_date) {
	//05/13/1952 m d y
	//return substr($mdy_date,6,4) ."-".substr($mdy_date,0,2) ."-".substr($mdy_date,3,2);
	if ($mdy_date!='') {
		$d_arr = split("-",$mdy_date);
		return "$d_arr[2]-$d_arr[0]-$d_arr[1]";
	}
	else
		return '';
}

function exitError($msg) {
	$btn = "<br><input type=button onclick='history.back()' value='<<Kembali'>";
	$m = "<br><br><br><br><br><fieldset style=\"width: 600px; height:300px;\"><legend>Ralat</legend><br><br><br><br><br><font color=red>$msg</red> $btn </fieldset>";
	$m = "<center>$m</center>";
	echo $m;exit;
}
function exitError3($error) {
	$btn = "<br><input type=button onclick='history.back()' value='<<Kembali'>";
		$str = '<center><div style=" text-align:center; background-color: #ffcccc; border-style: solid; border-width:thin; border-color: red; padding: 5px; margin-bottom:10px;">';
	 
		$str .= "$error\n";
		$str .= "</div> $btn</center>";
	echo $str;exit;
}


function isIncluded($e,$arr) {
	for ($i=0;$i<count($arr);$i++) {
		if ($arr[$i]==$e) return 1;
	}
	return 0;
}

function getHashedObject($fld,$pkey_field,$tbl) {
	global $DB;

	$sql = "SELECT distinct($fld),$pkey_field FROM $tbl";
	$res = $DB->Execute($sql);
	$num = $res->RecordCount();
	$arr = array();
	for ($i=0;$i<$num;$i++) {
		$dat = pg_fetch_array($res,$i);
		$arr[] = $dat;
	}
	return $arr;
}

function getList($fld,$tbl) {
	global $DB;

	$sql = "SELECT distinct($fld) FROM $tbl";
	$res = $DB->Execute($sql);
	$num = $res->RecordCount();
	$arr = array();
	for ($i=0;$i<$num;$i++) {
		$dat = pg_fetch_array($res,$i);
		$arr[] = $dat[$fld];
	}
	return $arr;
}

function makeIntegerHash($num_start,$num_end) {
	$arr = array();
	for ($i=$num_start;$i<=$num_end;$i++) {
		$arr[] = $i;
	}
	return $arr;
}

function getOptionsFromTable($tblname,$key,$value='') {
	global $DB;

	if ($value!='') $sql = "SELECT distinct($key),$value FROM $tblname";
	else $sql = "SELECT distinct($key) FROM $tblname";
	
	$res = $DB->Execute($sql);
	$num = $res->RecordCount();
	for ($i=0;$i<$num;$i++) {
		$dat = $res->fields;
		if ($value!='') {
			$k = $dat[strtoupper($value)];
			$v = $dat[strtoupper($key)];
			$k = $dat[$value];
			$v = $dat[$key];
			//$arr[strtoupper($k)] = $v;  //to make capital letter all the options
			$arr[$k] = $v;
		}
		else {
			//$v = $dat[strtoupper($key)];
			$v = $dat[$key];
			$arr[] = $v;
		}
		
		$res->MoveNext();
	}
	//print_r($arr);exit;
	return $arr;
}

function getOptionsFromTableWithConds($tblname,$key,$value,$where) {
	global $DB;

	$sql = "SELECT distinct($key),$value FROM $tblname $where";
	$res = $DB->Execute($sql);
	$num = $res->RecordCount();
	$arr = array();

	for ($i=0;$i<$num;$i++) {
		$dat = $res->fields;
		//print_r($dat);exit;
		if ($value!='') {
			$k = $dat[strtoupper($value)];
			$v = $dat[strtoupper($key)];
			//$arr[strtoupper($k)] = $v;
			$arr[$k] = $v;
		}
		else {
			//$v = $dat[strtoupper($key)];
			$v = $dat[$key];
			$arr[] = $v;
		}
		
		$res->MoveNext();
	}
	return $arr;
}

function getOptionsFromTableWithConds2($tblname,$key,$value,$where_key='',$where_value='') {
	global $DB;
	
	if (($where_key=='') && ($where_value=='')) {
		$where = '';
	}
	else {
		$where = "WHERE $where_key='$where_value'";
	}

	$sql = "SELECT distinct($key),$value FROM $tblname $where";
	$res = $DB->Execute($sql);
	$num = $res->RecordCount();
	$arr = array();

	for ($i=0;$i<$num;$i++) {
		$dat = $res->fields;
		//print_r($dat);exit;
		if ($value!='') {
			$k = $dat[strtoupper($value)];
			$v = $dat[strtoupper($key)];
			$arr[$k] = $v;
		}
		else {
			$v = $dat[strtoupper($key)];
			$arr[] = $v;
		}
		
		$res->MoveNext();
	}
	return $arr;
}

function getDistinctValueAsList($val) {
	global $DB;

	$sql = "SELECT distinct($val) FROM vw_lawatan_flat ORDER BY $val";
	$res = $DB->Execute($sql);
	$num = $res->RecordCount();
	$arr = array();
	for ($i=0;$i<$num;$i++) {
		$dat = pg_fetch_array($res,$i);
		$arr[] = $dat[$val];
	}
	return $arr;
}

//flash some message
function alert_some_message() {
	global $DB;

	$flash = $_SESSION['flash'];

	if ($flash!='') {
		$str = '<center><div style=" text-align:center; background-color: #ccffcc; border-style: solid; border-width:thin; border-color: green; padding: 5px; margin-bottom:10px;">';
	 
		$str .= "$flash\n";
		$str .= "</div></center>";
		echo $str;
	}
	
	$_SESSION['flash'] = '';	
}

//flash some error
function alert_some_error() {
	global $DB;

	$error = $_SESSION['error'];

	if ($error!='') {
		$str = '<center><div style=" text-align:center; background-color: #ffcccc; border-style: solid; border-width:thin; border-color: red; padding: 5px; margin-bottom:10px;">';
	 
		$str .= "$error\n";
		$str .= "</div></center>";
		echo $str;
	}
	
	$_SESSION['error'] = '';	
}

function alert_some_warning() {
	global $DB;

	$warning = $_SESSION['warning'];

	if ($warning!='') {
		$str = '<center><div style=" text-align:center; background-color: #ffff66; border-style: solid; border-width:thin; border-color: #ff9900; padding: 5px; margin-bottom:10px;">';
	 
		$str .= "$warning\n";
		$str .= "</div></center>";
		echo $str;
	}
	
	$_SESSION['warning'] = '';	
}

function getObjectCategoryIdFromName($table_name) {
	global $DB;

	$sql = "SELECT * FROM object_categories WHERE table_name ilike '$table_name'";
	$res = $DB->Execute($sql);
	$dat = pg_fetch_object($res,0);
	return $dat->id;
}

function getNameFromId($table_name,$id) {
	global $DB;

	if ($id == '') return '';
	$sql = "SELECT name FROM $table_name WHERE id = '$id'";
	$res = $DB->Execute($sql);
	$dat = pg_fetch_object($res,0);
	return $dat->name;	
}

function getFieldFromId($table_name,$field_name,$id) {
	global $DB;
	$field_name = strtoupper($field_name);
	
	if ($id == '') return '';
	$sql = "SELECT $field_name FROM $table_name WHERE id = '$id'";
	$res = $DB->Execute($sql);
	if ($res->RecordCount() < 1) return '';
	return $res->fields[$field_name];
}


function getFieldByWhereSql($table_name,$field_name,$where_clause) {
	global $DB;
	//$field_name = strtoupper($field_name);

	$sql = "SELECT $field_name FROM $table_name $where_clause";
	$res = $DB->Execute($sql);
	return $res->fields[$field_name];
}


function getStates() {
	global $DB;
	$sql = "SELECT * FROM states ORDER BY id";
	$res = $DB->Execute($sql);
	$hash = array();
	while ($dat = pg_fetch_object($res)) {
		$hash[(string)$dat->id] = $dat->name;
	}
	return $hash;	
}

function getItemsOfGroup($group_id) {
	global $DB;
	$sql = "SELECT * FROM items WHERE item_group_id='$group_id' ORDER BY id";
	$res = $DB->Execute($sql);
	$hash = array();
	while ($dat = pg_fetch_object($res)) {
		$hash[(string)$dat->id] = $dat->name;
	}
	return $hash;
}



function decapitalize($hash) {
	foreach ($hash as $K => $v) {
		$k = strtolower($K);
		$hash->$k = $v;
	}
	return $hash;
}

function get_latest_no_ahli() {
	global $DB;
	$sql = "SELECT max(no_ahli) as latest_no_ahli FROM kunci_induk";
	$res = $DB->Execute($sql);
	$dat = $res->FetchnextObject();
	$dat = decapitalize($dat);
	return $dat->latest_no_ahli;
}

function doPayment($ahli_id,$jenis_payment,$sesi_tahun,$bulan,$jum_pokok) {
	global $DB;
	$payment = new Payment();
	if ($payment->Load("ahli_id=$ahli_id AND month=$bulan AND sesi_tahun='$sesi_tahun'")) { //exist already
		switch ($jenis_payment) {
			case "1001": //if jum_bayaran = 10 ==(6+3+1)
				if ($jum_pokok != 10) {
					$payment->yuran_amount = $jum_pokok;
				}
				else {
					$payment->yuran_amount     = '6';			
					$payment->sumbangan_amount = '3';
					$payment->tabung_amount    = '1';
				}
				break;
			case "1002":
				$payment->sumbangan_amount = ($jum_pokok=='')?NULL:$jum_pokok;			
				break;
			case "0501": //insuran kot.. kena tanya irou
				$payment->insuran_amount = $jum_pokok;
				break;
			default:
				$payment->sumbangan_amount = NULL;
				break;
		}
		//$payment->insuran_amount = ($i=='')?NULL:$i;
		if ($payment->save()) {
			return true;
		}
		//echo "already exist";exit;
	}
	else { //not exist yet
		$payment->ahli_id = $ahli_id;
		$payment->month   = $bulan;
		$payment->sesi_tahun = $sesi_tahun;
		switch ($jenis_payment) {
			case "1001":
				if ($jum_pokok != 10) {
					$payment->yuran_amount = $jum_pokok;
				}
				else {
					$payment->yuran_amount     = '6';			
					$payment->sumbangan_amount = '3';
					$payment->tabung_amount    = '1';
				}
				break;
			case "1002":
				$payment->sumbangan_amount = ($jum_pokok=='')?NULL:$jum_pokok;			
				break;
			case "0501": //insuran kot.. kena tanya irou
				$payment->insuran_amount = $jum_pokok;
				break;
			default:
				$payment->sumbangan_amount = NULL;
				break;
		}
		$payment->date_paid = Date("Y-m-d");
		if ($payment->save()) {
			return true;
		}
	}
	return false;
	
}

// gmi added 27/01/2011
function get_tunggakan_ahli($no_ahli, $tahun, $bulan){
	global $DB;
	$t_format = '%s-%02d-01';
	$tarikh = sprintf($t_format, $tahun, $bulan);
	$sql = "SELECT (SUM(wajib_bayar) - SUM(dibayar)) as tunggakan FROM vw_penyata_payment 
				WHERE no_ahli = $no_ahli
				AND bayaran_untuk_bulan < '$tarikh' 
				 
			";
	//echo $sql; exit;--AND date_paid < '$tarikh'
	//echo $sql."<br/>";
	$res = $DB->Execute($sql);
	if(!$res) return 0;
	$d = $res->FetchNextObject();
	//echo ">>>>".$d->TUNGGAKAN;
	if(is_null($d->TUNGGAKAN)) return 0;
	return $d->TUNGGAKAN;
}

// yuran yg telah dibayar utk bulan tersebut ...
function get_yuran_dibayar($no_ahli, $tahun, $bulan){
	global $DB;
	$bulan = sprintf("%02d",$bulan);
	$sql = "SELECT SUM(dibayar) as dibayar FROM vw_penyata_payment 
				WHERE (
					no_ahli = $no_ahli
					AND sesi_tahun  '$tahun' 
					AND bulan  '$bulan'
				)
			";
	//echo $sql; exit;
	$res = $DB->Execute($sql);
	if(!$res) return 0;
	$d = $res->FetchNextObject();
	return $d->DIBAYAR;
}

// yuran yg sudah dibayar
function get_sudah_dibayar($no_ahli, $tahun, $bulan){
	global $DB;
	$bulan = sprintf("%02d",$bulan);
	$sql = "SELECT dibayar FROM vw_penyata_payment 
				WHERE(
					no_ahli = $no_ahli
					AND sesi_tahun = '$tahun' 
					AND month  = '$bulan'
				)
			";
	//echo $sql."<br>";
	$res = $DB->Execute($sql);
	if(!$res) return 0;
	$d = $res->FetchNextObject();
	if(is_null($d->DIBAYAR)) return 0;
	return $d->DIBAYAR;
}

// Yuran wajib bayar untuk bulan tersebut...
// 
//
function get_wajib_bayar($no_ahli, $tahun, $bulan){
	global $DB;
	$bulan = sprintf("%02d",$bulan);
	$sql = "SELECT wajib_bayar FROM vw_penyata_payment 
				WHERE(
					no_ahli = $no_ahli
					AND sesi_tahun  '$tahun' 
					AND bulan  '$bulan'
				)
			";
	$res = $DB->Execute($sql);
	if(!$res) return 0;
	$d = $res->FetchNextObject();
	return $d->DIBAYAR;
}
?>
